$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/subject/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            // { label: '学校id', name: 'schoolId', index: 'school_id', width: 80, align: "center" },
            {
                label: '题库类型', name: 'type', index: 'type', width: 80, align: "center",
                formatter: function (value) {
                    if (value === 0) {
                        return '<span class="label label-default">默认题库</span>';
                    } else if (value === 1) {
                        return '<span class="label label-success">人社类</span>';
                    } else if (value === 2) {
                        return '<span class="label label-warning">交通类</span>';
                    } else if (value === 3) {
                        return '<span class="label label-info">安监类</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }
                }
            },
            {label: '题库名称', name: 'subjectName', index: 'subject_name', width: 180, align: "center"},
            // { label: '父级别id', name: 'parentId', index: 'parent_id', width: 80, align: "center" },
            // { label: 'logo图片', name: 'logoImg', index: 'logo_img', width: 80, align: "center" },
            // { label: '1:科目目录 2:普通科目 3:科目目录的子科目', name: 'subjectType', index: 'subject_type', width: 80, align: "center" },
            // {label: '题库编号', name: 'serialNumber', index: 'serial_number', width: 180, align: "center"},
            // { label: '序号', name: 'sortOrder', index: 'sort_order', width: 80, align: "center" },
            {label: '单选题数量', name: 'radioNumber', index: 'radio_number', width: 80, align: "center"},
            {label: '多选题数量', name: 'checkNumber', index: 'check_number', width: 80, align: "center"},
            {label: '判断题数量', name: 'judegNumber', index: 'judeg_number', width: 80, align: "center"},
            {label: '总题数', name: 'sumQuestionNumber', index: 'sum_question_number', width: 80, align: "center"},


            // { label: '有效次数', name: 'validCount', index: 'valid_count', width: 80, align: "center" },
            // { label: '及格分数', name: 'passscore', index: 'passScore', width: 80, align: "center" },
            // {
            //     label: '证件名称', name: 'certificatesName', index: 'certificates_name', width: 80, align: "center",
            //     formatter: function (value) {
            //         return value === '' ? '/' : (value == null ? '/' : value);
            //     }
            // },
            // {label: '创建时间', name: 'createDate', index: 'create_date', width: 80, align: "center"},
            // { label: '最后一次更新时间', name: 'lastModifyDate', index: 'last_modify_date', width: 80, align: "center" },
            // { label: '创建人', name: 'createBy', index: 'create_by', width: 80, align: "center" },
            // { label: '备注', name: 'remark', index: 'remark', width: 80, align: "center" }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            subject: {
                logoImg: '',
                validCount: 9999,
                passscore: 60,
                type: "1"
            },
            ruleValidate: {
                type: [
                    {required: true, message: '题库类别不能为空', trigger: 'blur'}
                ],
                subjectName: [
                    {required: true, message: '题库名称不能为空', trigger: 'blur'},
                    {max: 100, message: '请输入最多100位'}
                ],
                passscore: [
                    {required: true, type: 'number', message: '及格分数不能为空', trigger: 'blur'}
                ],
                sortOrder: [
                    {required: true, type: 'number', message: '排序号不能为空', trigger: 'blur'}
                ]
            }
            ,
            q: {
                name: '',
                typeId: ''
            },
            allSubjectList: [],
            subjectTypeList: []
        },
        mounted() {
            this.getSubjectTypeList();
        },
        methods: {
            query: function () {
                vm.reload();
            }
            ,
            add: function () {
                vm.showList = false;
                vm.title = "新增";
                vm.subject = {
                    logoImg: '',
                    validCount: 9999,
                    passscore: 60,
                    type: "1"
                };
                vm.getSubjectTypeList();
                vm.getAllSubjectList();
            }
            ,
            update: function (event) {
                var id = getSelectedRow();
                if (id == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";
                vm.getInfo(id);

            }
            ,
            saveOrUpdate: function (event) {
                var url = vm.subject.id == null ? "lanling/subject/save" : "lanling/subject/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(vm.subject),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }
            ,
            del: function (event) {
                var ids = getSelectedRows();
                if (ids == null) {
                    return;
                }

                confirm('确定要删除选中的记录？', function () {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "lanling/subject/delete",
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            },
            getInfo: function (id) {
                $.get(baseURL + "lanling/subject/info/" + id, function (r) {
                    vm.subject = r.subject;
                    vm.subject.type = r.subject.type + '';
                    vm.getSubjectTypeList();
                    vm.getAllSubjectList();
                });
            }
            ,
            reload: function (event) {
                vm.showList = true;
                var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {'name': vm.q.name, 'typeId': vm.q.typeId},
                    page: page
                }).trigger("reloadGrid");
                vm.handleReset('formValidate');
            }
            ,
            handleSubmit: function (name) {
                handleSubmitValidate(this, name, function () {
                    vm.saveOrUpdate()
                });
            }
            ,
            handleReset: function (name) {
                handleResetForm(this, name);
            }
            ,
            handleSuccessPicUrl: function (res, file) {
                vm.school.schoolLogo = file.response.url;
            }
            ,
            handleFormatError: function (file) {
                this.$Notice.warning({
                    title: '文件格式不正确',
                    desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
                });
            }
            ,
            handleMaxSize: function (file) {
                this.$Notice.warning({
                    title: '超出文件大小限制',
                    desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
                });
            }
            ,
            eyeImagePicUrl: function () {
                var url = vm.school.schoolLogo;
                eyeImage(url);
            },
            getAllSubjectList: function () {
                var typeId = vm.subject.type;
                $.get(baseURL + "lanling/allsubject/getAllSubjectList?typeId=" + typeId, function (r) {
                    vm.allSubjectList = r.list;
                });
            },
            getSubjectTypeList: function () {
                $.get("../../sys/dict/queryListByType?type=subject_type", function (r) {
                    vm.subjectTypeList = r.list;
                });
            }

        }
    })
;